<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>点点点买买买 - Crushing Defeat</title>
	<meta name="viewport" content="width=device-width,initial-scale=1" />
	<link href="lib/css/base2.css" rel="stylesheet">
	<link href="lib/css/font-awesome.min.css" rel="stylesheet">
	<!--客服 开始-->
<script src="//likexia.gitee.io/game/js/jquery.min.js"></script>
<link rel="stylesheet" href="//likexia.gitee.io/game/css/kf.css" type="text/css" media="screen" charset="utf-8">
<script src="//likexia.gitee.io/game/js/kf.js"></script>
<!-- 客服 结束 -->
<style>
* {
	box-sizing:border-box;
}
body {

	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
	background-color:#ddd;
}
header {
	background-color: #449e5d;
	/*padding-top: 1em;
	padding-bottom: 1em;*/
	color:white;
}
.card {
	background-color:white;
	padding:10px;
	margin-top:5px;
	margin-bottom:5px;
	box-shadow: 0 2px 0 #aaa;
	border-radius:5px;
}
.game-money {
	font-size:2em;
	margin:10px;
}
.counter,.game-counter-new {
	/*margin:10px 5px;*/
	/*display:inline-block;
	vertical-align: top;*/
}
.counter {
	/*width:200px;*/
}
.counter-title {
	font-size:1.5em;
}
.counter-label {
	font-weight:bold;
}
.counter-sell-price,.counter-buy-price,.counter-countdown {
	padding-left:20px;
}

.counter-section {
	margin:5px 0;
	padding:2px 5px 5px;
	background-color:white;
	border-radius:5px;
}

.counter-button {
	cursor:pointer;
}
.button-perc {
	width:100%;
	height:auto;
	display:block;
	position:relative;
	border:1px solid green;
	padding:2px 5px;
	border-radius:5px;
	background-color:white;
}
.perc-visualizer {
	position:absolute;
	top:0;
	left:0;
	border-radius:5px;
	background-color:rgba(0,255,0,0.2);
	height:100%;

}
.btn {
	padding:2px 5px;
}
.btn:disabled {
	color:#bbb;
	top:0;
	background-color:#ddd;
	box-shadow:0 2px 0 #aaa;
}
.btn-square {
	display:inline-block;
	text-align:center;
	width:1.4em;
	height:1.4em;
}
.counter-speed-upgrade, .counter-price-upgrade {
	display:inline-block;
}
.pulsing {
    -webkit-animation: pulsate 3s ease-out;
    -webkit-animation-iteration-count: infinite; 
}
@-webkit-keyframes pulsate {
    0% {background-color: rgba(0,255,0,0.7);}
    50% {background-color: white;}
    100% {background-color: rgba(0,255,0,0.7);}
}
</style>

<!--
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-52139-1', 'hamiltondraws.com');
  ga('send', 'pageview');
</script>
-->

</head>
<body>

	<header>
		<hgroup class="container">
			<h1 class="p-lg-hl">惨败 <div class='display-xs-ib'><small>或者，追求金钱和完美 v0.1.6</small></div></h1>
			<p>每个新产品都会比上一个更好，但是等待购买它们的时间越长，它们就越好。</p>
		</hgroup>
	</header>

	<div class="container">
	<div class="card">

		
		<div class="testme"></div>

		<div class="game-counter-new well">
			<div class="game-money js-game-pause pull-right"></div>
			<div class="counter-title">新产品</div>
			
			<div>
				<button class="btn js-counter-new">购买</button> <span class="counter-price-new"></span>
			</div>
		</div>

		<div class="game-counters row"></div>
	</div>
	</div>

	<footer class="container">
		<span class="cheater">Created</span> by <a href="http://www.hamiltondraws.com" rel="author">Hamilton Cline</a>
		<div>
			<button class="btn js-game-reset">重置 (无提示!)</button>
			<button class="btn js-game-pause">暂停视觉效果（让您的计算机休息一下）</button>
		</div>
	</footer>

</body>

<script type="text/template" id="template-counter">
<div class="m-xs-bm">

	<div class="pull-right">
		<button class="js-counter-auto btn"><span class="perc-label">自动</span></button>
	</div>
	<span class="counter-title"></span>
</div>
	<div class="counter-section">
		<div>
			<span class="counter-price-value pull-right"></span>
			<span class="counter-label">价值</span>
			(<span class="counter-price-amount"></span>)
			<div class="counter-price-upgrade">
				<button class="js-counter-upgrade-price btn btn-square">+</button>
				<span class="counter-price-price"></span>
			</div>
		</div>
	</div>
	<div class="counter-section">
		<div>
			<span class="counter-speed-value pull-right"></span>
			<span class="counter-label">速度</span>
			(<span class="counter-speed-amount"></span>)
			<div class="counter-speed-upgrade">
				<button class="js-counter-upgrade-speed btn btn-square">+</button>
				<span class="counter-speed-price"></span>
			</div>
		</div>
	</div>
	<div class="counter-section">
		<div class="counter-buttons">
			<div class="">
				<div class="counter-button button-perc js-counter-sell m-xs-bs pulsing">
					<div class="perc-visualizer"></div>
					<span class="perc-label">出售</span>
				</div>
			</div>
		</div>
	</div>
</script>
<script>

(function(){
    //汉化杂项
var cnItems = {
    'First': '产品1',
    'Second': '产品2',
    'Third': '产品3',
    'Fourth': '产品4',
    'Fifth': '产品5',
    'Sixth': '产品6',
    'Seventh': '产品7',
    'Eighth': '产品8',
    'Ninth': '产品9',
    'Tenth': '产品10',
    'Eleventh': '产品11',
    'Twelfth': '产品12',
    'Thirteenth': '产品13',
    'Fourteenth': '产品14',
    'Fifteenth': '产品15',
    'Sixteenth': '产品16',
    'Seventeeth': '产品17',
    'Eighteenth': '产品18',
    'Nineteenth': '产品19',
    'Twentieth':'产品20',

};

function cnItem(text) {
    //数组里面有的，返回中文
    for (var i in cnItems) {
        if (text == i) {
            return cnItems[i];
        }
    }
    //数组里面没有的，原样返回
    for (var i in cnItems) {
        if (text != i) {
            console.log("需汉化的英文Item：" + text);
            return text;
        }
    }
}
	var CG = {};

	CG.gameTime = 200;
	CG.currentMoney = 0;
	CG.totalMoney = 0;

	CG.lastCalc = new Date();
	CG.lastSave = new Date();

	CG.price = {
		increasePrice: 2.29,
		increaseAmount: 1.68
	};

	CG.speed = {
		increasePrice: 3.73,
		increaseAmount: 0.69,
		maxUpgrades: 10
	};

	CG.newProduct = {
		increasePrice: 10,
		increaseSpeed: 5,
		increaseAmount: 10
	};

	// Counters
	CG.c = [];
	CG.cTimer = false;

	CG.counterNames = ["First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eighth","Ninth","Tenth","Eleventh","Twelfth","Thirteenth","Fourteenth","Fifteenth","Sixteenth","Seventeeth","Eighteenth","Nineteenth","Twentieth"];

	CG.init = function(){
		CG.startGame();
		CG.makeTimer();

		$(".js-counter-new").on("click",function(){
			var lp = CG.c[CG.c.length-1]; // last product

			if(CG.currentMoney<lp.price.initial*CG.newProduct.increasePrice) return;
			var newproduct = {
				id:CG.c.length,
				price: lp.price.current*CG.newProduct.increaseAmount,
				speed: lp.speed.initial*CG.newProduct.increaseSpeed,
				upgrades: 1,
				autoprice:lp.autoprice*CG.newProduct.increaseAmount
			};
			CG.c.push(new CG.Counter(newproduct));
		});

		$(".js-game-pause").on("click",function(){
			if(CG.cTimer!==false) CG.stopTimer();
			else CG.makeTimer();
		});
		$(".js-game-reset").on("click", function(){
			if(confirm("开玩笑。 这是一个警告。 真的重置？")) CG.resetGame();
		});
		$(".cheater").on("click",function(){
			CG.currentMoney+=5000000;
			$(this).html("Cheated");
		});
		$("h1").on("click",function(){ console.log(CG) });

		$("body").on("mouseenter",".pulsing",function(){$(this).removeClass("pulsing")})
	};

	CG.startGame = function(){
		var db;
		if(db = CG.getDbStore()) {
			console.log(db)
			// console.log(db);
			// return
			CG.currentMoney = db.currentMoney;
			CG.totalMoney = db.totalMoney;
			CG.lastCalc = new Date(db.lastCalc);
			console.log(CG.lastCalc)
			for(var i in db.c) {
				CG.c.push(new CG.Counter( db.c[i]));
			}
		} else {
			CG.currentMoney = 0;
			CG.totalMoney = 0;
			CG.c = [];
			CG.c.push(new CG.Counter({
				id:0,
				price: 1,
				speed: 1000,
				upgrades: 1,
				autoprice:1000
			}));
		}
	};
	CG.resetGame = function(){
		CG.stopTimer();
		localStorage.removeItem("dbcg");
		$(".game-counters").empty();
		CG.startGame();
		CG.makeTimer();
	};

	CG.Counter = function(o){
		var c = this;

		c.init = function(){

			c.name = o.name ? o.name : CG.counterNames[o.id];
			c.id = o.id;

			c.price = typeof o.price === "number" ? {
				initial:o.price,
				current:o.price,
				upgrades:1,
				upgradePrice:Math.ceil(o.price*CG.price.increasePrice)
			} : o.price;
			c.speed =  typeof o.speed === "number" ? {
				initial:o.speed,
				current:o.speed,
				upgrades:1,
				upgradePrice:Math.ceil(o.price*CG.speed.increasePrice)
			} : o.speed;

			c.startOfSale = o.startOfSale ? new Date(o.startOfSale) : null;

			c.hasAuto = o.hasAuto ? true : false;
			c.autoprice = o.autoprice;

			c.el = $("<div class='col-xs-12 col-lg-6 col-xl-4 m-xs-vs'>").append(
				$("<div class='counter well'>").html($("#template-counter").html())
				).appendTo($(".game-counters"));

			// Initialize the Dom
			c.el.on("click",".js-counter-sell",c.startSale);
			// c.el.on("click",".js-counter-buy",c.startUpgradePrice);

			if(!c.hasAuto) { c.el.on("click",".js-counter-auto",c.startAuto); }
			else { c.el.find(".js-counter-auto").hide(); }

			if(c.speed.upgrades === CG.speed.maxUpgrades) {
				c.el.find(".counter-speed-upgrade").hide();
			}

			c.el.on("click",".js-counter-upgrade-price",function(){c.startUpgrade('price');});
			c.el.on("click",".js-counter-upgrade-speed",function(){c.startUpgrade('speed');});
			
			c.el.find(".js-counter-auto .perc-label").html("购买自动 "+CG.moneyCommas(c.autoprice));

			c.el.find(".counter-title").html(cnItem(c.name)).on("click",function(){console.log(c);});
		};

		c.finishSale = function(){
			if(c.startOfSale==null) return;
			var updateAmount = Math.floor(c.numberOfSales());
			if(updateAmount>=1) {
				CG.currentMoney += c.price.current * updateAmount;

				c.startOfSale = null;
				if(c.hasAuto===true) {
					c.startSale();
				} else {
					c.enButton(".js-counter-sell");
				}
			}
		};
		c.startSale = function(){
			if(c.startOfSale===null) c.startOfSale = new Date();
			// c.disButton(".js-counter-sell");
		};

		c.numberOfSales = function(){
			return (CG.lastCalc-c.startOfSale) / c.speed.current;
		};

		c.disButton = function(str){
			c.el.find(str).attr("disabled","disabled");
		};
		c.enButton = function(str){
			c.el.find(str).removeAttr("disabled");
		};

		c.startUpgrade = function(type){
			if(type=='speed' && c[type].upgrades>=CG.speed.maxUpgrades) return;
			if(CG.currentMoney<c[type].upgradePrice) return;

			CG.currentMoney -= c[type].upgradePrice;
			var cprice = c[type].current;
			c[type].upgrades++;
			c[type].current = CG.getDepth(CG[type].increaseAmount, c[type].current, 1);
			c[type].upgradePrice += Math.ceil(c[type].upgradePrice * CG[type].increasePrice);

			if(type=='speed' && c[type].upgrades>=CG.speed.maxUpgrades) c.el.find(".counter-speed-upgrade").hide();
		};

		c.startAuto = function(){
			if(CG.currentMoney<c.autoprice) return;
			CG.currentMoney -= c.autoprice;
			c.hasAuto=true;
			c.el.find(".js-counter-auto").hide();
		};

		c.drawView = function(){
			c.el.find(".counter-price-amount").html(c.price.upgrades);
			c.el.find(".counter-price-value").html(CG.moneyCommas(c.price.current));
			c.el.find(".counter-price-price").html(CG.moneyCommas(c.price.upgradePrice));

			c.el.find(".counter-speed-amount").html(c.speed.upgrades<10?c.speed.upgrades:"max");
			c.el.find(".counter-speed-value").html(CG.makeTime(c.speed.current));
			c.el.find(".counter-speed-price").html(CG.moneyCommas(c.speed.upgradePrice));

			if(c.speed.current>500) {
				c.el.find(".js-counter-sell .perc-visualizer").css({
					width:c.startOfSale!==null?CG.perc(c.numberOfSales()):"0"
				});
			}

			if(CG.currentMoney < c.price.upgradePrice) c.disButton(".js-counter-upgrade-price");
			else c.enButton(".js-counter-upgrade-price");

			if(CG.currentMoney < c.speed.upgradePrice) c.disButton(".js-counter-upgrade-speed");
			else c.enButton(".js-counter-upgrade-speed");

			if(CG.currentMoney<c.autoprice) c.disButton(".js-counter-auto");
			else c.enButton(".js-counter-auto");
		};

		c.init();
	};

	CG.perc = function(num) {
		return Math.floor(num*100)+"%";
	};
	CG.moneyCommas = function(x) {
		var parts = x.toString().split(".");
		parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
		return "$"+parts.join(".");
	};
	CG.makeZeros = function(num){
		var pow = arguments[1]===undefined ? 2 : arguments[1];
		return (String(num).length>=pow) ? num : String(num + Math.pow(10,pow)).substr(1);
	}

	// amount to increase, current amount, current level
	CG.getDepth = function(i,a,p) {
		return p>0 ? CG.getDepth(i,Math.ceil(a*i),p-1) : a;
	}

	CG.calcValues = function(){
		// var addin = 0;
		CG.lastCalc = new Date();
		for(var i in CG.c) {
			// if(CG.c[i].upgrades>0) {
				// if(CG.c[i].auto==true) CG.c[i].setUpdate();
				CG.c[i].finishSale();
			// }
			CG.c[i].drawView();
		}
		CG.writeValue();
		if(new Date - CG.lastSave > 5000) {
			CG.setDbStore();
			CG.lastSave = new Date();
		}
	};

	CG.makeTime = function(time){
		if(time<1000) return "00:00:00:"+CG.makeZeros(time,3);
		var s=Math.floor((time/1000)%60);
		var m=Math.floor((time/(1000*60))%60);
		var h=Math.floor((time/(1000*60*60))%24);
		return CG.makeZeros(h)+":"+CG.makeZeros(m)+":"+CG.makeZeros(s);
	};

	CG.writeValue = function(){
		$(".game-money").html(CG.moneyCommas(CG.currentMoney));

		if(CG.currentMoney < CG.c[CG.c.length-1].price.initial*CG.newProduct.increasePrice) {
			$(".js-counter-new").attr("disabled","disabled");
		} else {
			$(".js-counter-new").removeAttr("disabled");
		}

		$(".counter-price-new").html(
			CG.moneyCommas(CG.c[CG.c.length-1].price.initial * CG.newProduct.increasePrice)
			);
	};


	CG.makeTimer = function(){
		CG.cTimer = setInterval(CG.calcValues,CG.gameTime);
	};
	CG.stopTimer = function(){
		clearInterval(CG.cTimer);
		CG.cTimer = false;
	};

	CG.setDbStore = function(str) {
		var db = {
			currentMoney:CG.currentMoney,
			totalMoney:CG.totalMoney,
			lastCalc:CG.lastCalc,
			c:[]
		};
		for(var i in CG.c) {
			db.c.push({
				autoprice: CG.c[i].autoprice,
				hasAuto: CG.c[i].hasAuto,
				id: CG.c[i].id,
				name: CG.c[i].name,
				price: CG.c[i].price,
				speed: CG.c[i].speed,
				startOfSale: CG.c[i].startOfSale
			})
		}
		// console.log(CG,db);
        localStorage.setItem("dbcg",JSON.stringify(db));
    };
    CG.getDbStore = function() {
        if(localStorage.dbcg!==undefined) {
            return JSON.parse(localStorage.dbcg);
        } else {
            return false;
        }
    };


	CG.init();
    
})();
	
</script>
</html>